(function($){
	
    $(function(){
    	
    	//查询活动
    	$('#searchActiveButton').click(function(e){
    		var flag = $("#searchActiveForm").validate(VALIDATE.searchActives).form();
    		if(flag){
    			$("#searchActiveForm").submit();
    		}
    		
    	});
        //删除活动链接
        $('.is-delete').click(function(e){
            e.preventDefault();
        	var activeId = $(this).parent().parent().parent().find('td').first().text();
        	$.ajax({
				  type: "POST",
				  url: ctx+"/activity/removeActivity.do",
				  data: "acId="+activeId,
				  success: function(msg){
					  if(msg.startWith("error:")){
						  showTips("出错了:"+msg.substring(6,msg.length));
					  }else{
						  showTips("执行成功", function (){
							  window.location.reload();
						  }, function () {
							  window.location.reload();
						  });
					  }
				  }
        	});
        });
        
        
    	//发布活动
		$('.publish-active-link').click(function(e){
			e.preventDefault();
			var activeId = $(this).parent().parent().parent().find('td').first().text();
			$.ajax({
				  type: "POST",
				  url: ctx+"/activity/publishActivity.do",
				  data: "acId="+activeId,
				  success: function(msg){
					  if(msg.startWith("error:")){
						  showTips("出错了:"+msg.substring(6,msg.length));
					  }else{
						  showTips("执行成功",function(){ window.location.reload();},
								  function(){
							  window.location.reload();
						  });
					  }
				  }
			});
		});
        
        //充值
        $('#rechargeGold').dialog({
            autoOpen: false,
            height: 300,
            width: 420,
            modal: true,
            buttons: {
                '确定充值': function(){
                  //判空
                	var accountNo = $('#accountNo').val();
                	var accountPass = $('#accountPass').val();
                	var rechargeMoney = $('#rechargeMoney').val();
                	var flag = $("#rechargeGoldForm").validate(VALIDATE.rechargeGold).form();
                	if(flag){
                		//ajax进行充值
                    	var acId = $('#acId').val();
                    	if(acId==""){
            				showTips("未获取到活动id");
            				return;
            			}
                    	$.ajax({
                    		type: "POST",
                    		url: ctx+"/activity/rechargeGold.do",
                    		data: "accountNo="+accountNo+"&accountPass="+accountPass+"&rechargeMoney="+rechargeMoney+"&acId="+acId,
                    		success: function(msg){
                    			if(msg.startWith("error:")){
                    				showTips("出错了:"+msg.substring(33,msg.length));
                    			}else{
                    				 showTips("执行成功",function(){window.location.reload();},
                    						 function(){
                    					 window.location.reload();
                    				 });
                    				$(this).dialog('close');
                    			}
                    		}
                    	
                    	});
                	}
					
                	
                },
                '取消': function(){
                    $(this).dialog('close');
                }
            }
        });
        
        //充值链接
        $('.recharge-gold-link').click(function(e){
            e.preventDefault();
            var activeId = $(this).parent().parent().parent().find('td').first().text();
            $('#acId').val(activeId);
            $('#rechargeGold').dialog('open');
        });
        
      //查看碎片
        $('#viewFragment').dialog({
            autoOpen: false,
            height: 600,
            width: 950,
            modal: true,
            buttons: {
                '确定': function(){
                    $(this).dialog('close');
                }
            }
        });
     
        //查看碎片链接
        $('.view-fragment-link').click(function(e){
            e.preventDefault();
            var activeId = $(this).parent().parent().parent().find('td').first().text();
            var jsURL = $('#jsUrl').val();
            
            $('#viewFragment').find('textarea:eq(2)').text(dealWithFrag(250,250,activeId,false,jsURL));
            $('#viewFragment').find('textarea:eq(3)').text(dealWithFrag(420,420,activeId,false,jsURL));
            $('#viewFragment').find('textarea:eq(4)').text(dealWithFrag(580,580,activeId,false,jsURL));
            $('#viewFragment').find('textarea:eq(5)').text(dealWithFrag(800,800,activeId,true,jsURL));
            $('#viewFragment').find('textarea:eq(6)').text(dealWithFrag(900,900,activeId,true,jsURL));
         
            $('#viewFragment').find('textarea').each(function (i) {
            	$(this).val($.trim($(this).val()));
            });
            $('#viewFragment').dialog('open');
        });
        
        //查看碎片复制
        $('#viewFragment').find('.item').find('button').click(function () {
        	var $textarea = $(this).siblings('textarea');
        	copyTxt($textarea);
        })
        
        //开启和关闭活动
        $('.active-close').click(function(e){
        	e.preventDefault();
        	var activeId = $(this).parent().parent().parent().find('td').first().text();
        	$.ajax({
				  type: "POST",
				  url: ctx+"/activity/closeOpenActivity.do",
				  data: "acId="+activeId,
				  success: function(msg){
					  if(msg.startWith("error:")){
						  showTips("出错了:"+msg.substring(33,msg.length));
					  }else{
						 showTips("执行成功",function(){window.location.reload();},
								 function(){
							 window.location.reload();
						 });//
					  }
				  }
        	});
        });
        
        //活动预览
        $('.check-preview').click(function () {
        })
    })
    function dealWithFrag(w,h,aid,flag,jsURL){
    	
    	   var scriptFrag = 
    		   	'<script src="http://ue1.17173.itc.cn/2011/17173my/js/jquery.xy-tips-window.2.8.js" type="text/javascript" charset="utf-8"></script>' +
		   		'<script src="http://ue1.17173.itc.cn/spp/spp_core.js" type="text/javascript" charset="GB2312"></script>' +
		   		'<script src="http://my.17173.com/js/spp_ui.js" type="text/javascript" charset="GB2312"></script>' +
				'<script type="text/javascript" src="'+jsURL+'/js/qb-fragment.js"></script>'+
				'<script type="text/javascript">'+
					'createQbFragment({' +
						'width: ' + w + ', ' +
						'height: ' + h + ', ' +
						'activeId: ' + aid + ', ' +
						'topWinner: ' + flag +
					'})' + 
				'</script>';
           	return scriptFrag;
    }

})(jQuery);